<?php

namespace App\Http\Controllers\admin\shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Store;
use App\Models\District;

use Intervention\Image\ImageManagerStatic as Image;

class StoreController extends Controller
{
    /**
     * **|--商家遍历列表页面--|***
     * @$list 获取全部商家的数据
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {

        $store = new Store();
        $list = $store->storeGet();

        //return json_decode($list);
        return view('admin.store.store',['list'=>$list]);
    }

    /**
     * **绑定添加商家的页面****
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.store.storeAdd');
    }

    /**
     * **绑定修改商家页面**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $data = Store::find($id);
        
        return view('admin.store.storeEdit',['data'=>$data]);
    }

    /**
     * **执行修改商家的方法***
     *
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|null
     */
    public function update($id,Request $request)
    {
        $store = new Store();
        $data = $request->except('_token','_method');

        $bool = $store->where('id',$id)->update($data);

        if($bool){
            return redirect('/admin/store');
        }else{
            return null;
        }

    }

    /**
     * **执行添加商家的方法***
     * @$data 取出商家全部信息
     * @$select 商家地址路径path
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $data = $request->only('name','status','username','address','info','phone','lng','lat');
        //dd($data);
        foreach ($data as $v){
            if(empty($v)){
                return back()->with('success','验证失败请重新填写,请按提示正确填写');
            }
        }
        $select = $request->input('select');
        $data['path'] = '';
        $data['credimg'] = '';
        foreach ($select as $v){
            if(is_numeric($v)){
                $data['path'] .= $v.',';
            }
        }
        //dd($data['path']);
        $data['credimg'] = $this->file($request);
        $data['password']=md5($request->input('password'));
        $store = Store::create($data);
   
        $bool = $store->save();
 
        if($bool){
            return redirect('/admin/store');
        }else{
            return back();
        }
    }

    /**
     * **药店状态控制**
     * @param Request $request
     * @return int
     */
    public function status(Request $request)
    {
        $id = $request->input('id');

        $data = Store::find($id);

        $data->status = $data->status==1?2:1;

        $bool = $data->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     ****修改商家logo****
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function editLogo(Request $request)
    {
        $id = $request->input('id');

        $data = Store::find($id);
        $img = Store::find($id)->logo;
        if(!empty($img) && file_exists('./upload/store/'.$img)){
            unlink('./upload/store/'.$img);
        }

        $data->logo = $this->file($request);

        $bool = $data->save();

        if($bool){
            return back();
        }

    }

    /**
     ***查看药店详情****
     * @param Request $request
     * @return mixed
     */
    public function detail(Request $request)
    {
        $data = Store::find($request->input('id'));

        return $data;
    }

    /**
     ***ajax访问行政区域****
     * @param Request $request
     * @return string
     */
    public function district(Request $request)
    {

        $district = new District;
        $list =  $district->dissel($request);

        return  json_encode($list);

    }

    /**
     ***处理图片的方法****
     * @param Request $request
     * @return string
     */
    public function file(Request $request)
    {
        //dd($request->hasFile('files'));
        if ($request->hasFile('photo')) {
            if ($request->file('photo')->isValid()) {
                $file = $request->file('photo');
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
                $img = Image::make($file);
                $img->resize(400, null, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $img->save('./upload/store/'.$picname);

                return $picname;

            }
        }
    }
}